package com.heisen.system.service.impl;

import com.heisen.base.entity.SystemUser;
import com.heisen.base.exception.RRException;
import com.heisen.base.utils.MyObjectUtils;
import com.heisen.database.service.impl.SuperServiceImpl;
import com.heisen.system.dto.UpdateUserPasswordDto;
import com.heisen.system.entity.Menu;
import com.heisen.system.entity.Role;
import com.heisen.system.entity.User;
import com.heisen.system.mapper.RoleMapper;
import com.heisen.system.mapper.UserMapper;
import com.heisen.system.query.RoleQuery;
import com.heisen.system.query.UserQuery;
import com.heisen.system.service.IMenuService;
import com.heisen.system.service.IRoleService;
import com.heisen.system.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author sen
 * @since 2023-12-24
 */
@Service
@Slf4j
public class UserServiceImpl  extends SuperServiceImpl<UserMapper, User, UserQuery> implements IUserService  {

    @Autowired
    IMenuService menuService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public boolean save(User user){

        User userByName = findByName(user.getUsername());
        if(MyObjectUtils.isNotEmpty(userByName))  throw new RRException("账号名重复");
        user.setPassword(passwordEncoder.encode(user.getPassword()));

        boolean save = super.save(user);

//        if(MyObjectUtils.isNotEmpty(user.getRoleId())){
//            UserRoleDto userRoleDto = new UserRoleDto();
//            userRoleDto.setUserId(sysUser.getUserId());
//            userRoleDto.setRoleId(addSysUserDto.getRoldIds());
//            Boolean aBoolean = userRoleService.addSysUserRole(userRoleDto);
//            if(!aBoolean) throw new PlatformException("用户 角色 关联失败");
//        }


        return  true ;
    }

    
    @Override
    public User findByName(String username) {
        log.info("【查找登入用户】  {}",username);
        User user = this.baseMapper.findByName(username);
        if(null != user) {
            String userPermissions = findUserPermissions(username);
            user.setPermissions(userPermissions);
        }
        return user;
    }

    @Override
    public String findUserPermissions(String username) {
        log.info("【查找用户权限】  {}",username);
        List<Menu> userPermissions = menuService.findUserPermissions(username);
        if(CollectionUtils.isEmpty(userPermissions)) return null ;
        return userPermissions.stream().map(Menu::getPerms).collect(Collectors.joining(","));
    }

    @Override
    public Boolean updateSysUserPass(UpdateUserPasswordDto updateUserPasswordDto) {
        User user = this.baseMapper.selectById(updateUserPasswordDto.getUserId());
        if(MyObjectUtils.isEmpty(user)) throw new RRException("改账号没找到");
        User logUserCopy = new User();
        BeanUtils.copyProperties(user,logUserCopy);
        user.setPassword(passwordEncoder.encode(updateUserPasswordDto.getPassword()));
        updateUserPasswordDto.setPassword(user.getPassword());
        return  this.updateById(user);
    }

}
